package leetcode.n2021;

import leetcode.n2021.bean.ListNode;

/**
 * 206. 反转链表
 * https://leetcode-cn.com/problems/reverse-linked-list/
 * @author Chaos
 * @version 1.0
 * @date 2020/11/2 15:05
 */
public class ReverseList {


    public ListNode reverseList(ListNode head) {
//        boolean flag = true;
//        ArrayList<Integer> list = new ArrayList<>();
//        while (flag) {
//            list.add(head.val);
//            if (head.next != null) {
//                head = head.next;
//            } else {
//                flag = false;
//            }
//        }
//
//
//        for (int i = list.size() - 1; i >= 0; i--) {
//            System.out.println(list.get(i));
//
//        }
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }




    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;


        new ReverseList().reverseList(listNode1);
    }
}
